/*
 * @描述:
 * @作者: 约訂
 * @Date: 2024-10-24 09:50:44
 * @LastEditTime: 2024-10-24 10:27:50
 * @修改人员: 约訂
 */
import { createStore } from "vuex";
// import websocketModule from "./modules/websocket";
// https://webpack.js.org/guides/dependency-management/#requirecontext

// const modulesFiles = require.context("./modules", true, /\.ts$/);
// // you do not need `import app from './modules/app'`
// // it will auto require all vuex module from modules file
// const modules = modulesFiles.keys().reduce((modules: any, modulePath: any) => {
//   // set './app.js' => 'app'
//   const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
//   const value = modulesFiles(modulePath);
//   (modules as any)[moduleName] = value.default;
//   return modules;
// }, {});
// const modules = import.meta
//   .globEager("./modules/**/*.ts")
//   .then((modules: any) => {
//     const newModules: any = {};
//     for (const modulePath in modules) {
//       const moduleName = modulePath.replace(/^\.\/modules\/(.*)\.\w+$/, "$1");
//       newModules[moduleName] = modules[modulePath].default;
//     }
//     return newModules;
//   });
const modulePaths = import.meta.glob("./modules/**/*.ts");
let modules: object = {};
Promise.all(
  Object.keys(modulePaths).map(async (modulePath) => {
    const moduleName = modulePath.replace(/^\.\/modules\/(.*)\.\w+$/, "$1");
    const module: any = await modulePaths[modulePath]();
    return { [moduleName]: module.default };
  })
).then((moduleALL) => {
  modules = moduleALL.reduce((acc, curr) => ({ ...acc, ...curr }), {});
  // 使用 mergedModules
});
export default createStore({
  state: {
    token:
      "6aCYSQYECMUCEbEqbATpRYUEOojaV0Wc712YNL7D4aGjajChEVtWTp/adPYCIeSRTDnYRJW5JJfhmbtXGw8J7IOky+ERPq6/",
  },
  getters: {},
  mutations: {
    setToken(state: any, data: any) {
      state.token = data;
    },
    getToken(state: any) {
      return state.token;
    },
  },
  actions: {},
  // modules: {
  //   websocket: websocketModule,
  // },
  modules: modules,
});
